package com.lqm.web.controller.home;

import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectSupport;
import com.lqm.entity.home.UserAddress;
import com.lqm.service.home.HomeUserService;
import com.lqm.service.home.ProjectSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-16 19:14
 */
@RestController
@RequestMapping("/home")
public class ProjectSupportController {
    @Autowired
    ProjectSupportService projectSupportService;
    @Autowired
    HomeUserService homeUserService;

    @RequestMapping("findSupportSumMoneyByProjectId")
    public Map<String, Object> findSupportSumMoneyByProjectId(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Project project = new Project();
        project.setProjectId(id);
        double sum = 0.0;

        try {
            sum = projectSupportService.findSupportSumByProject(project);
            map.put("currentMoney", sum);
            map.put("msg", "请求成功");
        } catch (Exception e) {
            map.put("msg", "请求失败");
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 获取项目支持人数
     *
     * @param id
     * @return
     */
    @RequestMapping("findSupportUserCount")
    public Map<String, Object> findSupportUserCount(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Project project = new Project();
        project.setProjectId(id);
        int sum = 0;
        try {
            sum = projectSupportService.findSupportUserCount(project);
            map.put("supportCount", sum);
            map.put("msg", "请求成功");
        } catch (Exception e) {
            map.put("msg", "请求失败");
        }

        return map;
    }

    /**
     * 投资某一个项目
     *
     * @param projectSupport
     * @param session
     * @return
     */

    @RequestMapping("support")
    public Map<String, Object> support(ProjectSupport projectSupport, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();

        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        projectSupport.setHomeUser(homeUser);
        projectSupport.setUsername(homeUser.getUsername());
        System.out.println(projectSupport);
        String support = projectSupportService.support(projectSupport, homeUser);
        Integer num = projectSupport.getNum();
        double price = Double.parseDouble(projectSupport.getPrice().toString());
        Double account = homeUser.getAccount();
        homeUser.setAccount(account - price * num);

        if (support != null) {
            //成功
            map.put("code", 0);
            session.setAttribute("home_user", homeUser);
            map.put("msg", support);
        } else {
            map.put("code", 1);
            map.put("msg", support);
        }
        return map;
    }

    /**
     * 获取一个项目的所有投资
     *
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @RequestMapping("getProjectSupport")
    public Map<String, Object> getProjectSupport(int page, int limit, Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Project project = new Project();
        project.setProjectId(id);
        List<ProjectSupport> allByProject = null;
        try {
            allByProject = projectSupportService.findAllByProject(page, limit, project);
            for (ProjectSupport support : allByProject) {
                HomeUser homeUser = support.getHomeUser();
                homeUser.setProjects(null);
                homeUser.setUserAddresses(null);
                support.setHomeUser(homeUser);
                support.getProject().setHomeUser(null);
            }
            Integer projectCount = projectSupportService.findAllByProjectCount(project);
            map.put("code", 0);
            map.put("data", allByProject);
            map.put("msg", "请求成功");
            map.put("count", projectCount);
        } catch (Exception e) {
            map.put("msg", "请求失败");
        }

        return map;
    }

    /**
     * 获取某个用户投资的所有条目
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("getProjectSupportByHomeUser")
    public Map<String, Object> getProjectSupportByHomeUser(int page, int limit, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        List<ProjectSupport> allByProject = null;
        try {
            allByProject = projectSupportService.findAllByHomeUser(page, limit, homeUser);
            for (ProjectSupport support : allByProject) {
                HomeUser user = support.getHomeUser();
                user.setProjects(null);
                user.setUserAddresses(null);
                user.setPassword(null);
                Project project = support.getProject();

                project.setDetail("");
                project.setHomeUser(null);
                support.setProject(project);
                support.setHomeUser(user);
            }
            Integer projectCount = projectSupportService.findAllByHomeUserCount(homeUser);
            map.put("code", 0);
            map.put("data", allByProject);
            map.put("msg", "请求成功");
            map.put("count", projectCount);
        } catch (Exception e) {
            map.put("msg", "请求失败");
        }

        return map;
    }

    @RequestMapping("sendSupport")
    public Map<String, Object> sendSupport(ProjectSupport projectSupport, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");

        ProjectSupport support = projectSupportService.sendSupport(projectSupport);
        if (support!=null){
            map.put("msg","项目发货成功");
        }else {
            map.put("msg","项目发货失败");
        }
        return map;
    }

    /**
     * 完成众筹
     *
     * @param projectSupport
     * @param session
     * @return
     */
    @RequestMapping("finishSupport")
    public Map<String, Object> finishSupport(ProjectSupport projectSupport, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");

        ProjectSupport support = projectSupportService.finishSupport(projectSupport);
        if (support!=null){
            map.put("msg","项目回报完成");
        }else {
            map.put("msg","项目回报失败");
        }
        return map;
    }


}
